import { PartialType } from '@nestjs/mapped-types';
import { Transform, Type } from 'class-transformer';
import { IsArray, IsBoolean, IsDate, IsEnum, IsNumber, IsOptional, IsString, Matches } from 'class-validator';
import { GenderEnum, SortOrderEnum, StatusEnum } from '@/common/enums';
import { useRegExp } from '@/common/hooks/useRegExp';

export class CreateUserDto {
  @Matches(useRegExp('username').regexp, { message: useRegExp('username').msg })
  username: string;

  @IsString()
  @IsOptional()
  nickname?: string;

  @Matches(useRegExp('password').regexp, { message: useRegExp('password').msg })
  password: string;

  @Matches(useRegExp('password').regexp, { message: useRegExp('password').msg })
  password2: string;

  @IsString({ message: '请输入图形验证码' })
  gcaptcha: string;

  // 邮箱绑定可选
  @IsString({ message: '请输入邮箱验证码' })
  @IsOptional()
  ecaptcha?: string;

  @IsBoolean()
  @IsOptional()
  needecaptcha?: boolean = false;

  @IsBoolean()
  @IsOptional()
  autologin?: boolean = false;

  @Matches(useRegExp('email').regexp, { message: useRegExp('email').msg })
  @IsOptional()
  email?: string;

  @Matches(useRegExp('phone').regexp, { message: useRegExp('phone').msg })
  @IsOptional()
  phone?: string;

  @IsString({ each: true })
  @IsOptional()
  roles?: string[];

  @IsEnum(StatusEnum)
  @IsOptional()
  status?: StatusEnum;

  @IsString()
  @IsOptional()
  avatar?: Record<string, any>;

  @IsString()
  @IsOptional()
  department?: string;

  @IsString()
  @IsOptional()
  signature?: string;

  @IsString()
  @IsOptional()
  remark?: string;

  @IsString({ each: true })
  @IsOptional()
  tags?: string[];

  @IsDate()
  @IsOptional()
  birthday?: Date;

  @IsEnum(GenderEnum)
  @IsOptional()
  gender?: GenderEnum;

  @IsString()
  @IsOptional()
  registerIp?: string;

  @IsString()
  @IsOptional()
  loginIp?: string;

  @IsDate()
  @IsOptional()
  registerTime?: Date;

  @IsDate()
  @IsOptional()
  loginTime?: Date;
}

export class LoginUserDto extends PartialType(CreateUserDto) {
  @IsNumber()
  @IsOptional()
  id?: number;

  @Matches(useRegExp('username').regexp, { message: useRegExp('username').msg })
  username: string;

  @Matches(useRegExp('password').regexp, { message: useRegExp('password').msg })
  password: string;

  @IsString({ message: '请输入图形验证码' })
  gcaptcha: string;
}

export class FindUserDto extends PartialType(CreateUserDto) {
  @IsNumber()
  @IsOptional()
  id?: number;

  @IsString()
  @IsOptional()
  username?: string;

  @IsString()
  @IsOptional()
  role?: string;

  @IsString()
  @IsOptional()
  sortField?: string = 'createTime';

  @IsEnum(SortOrderEnum) // 确保 sortOrder 只能是 'ASC'升序 或 'DESC'降序
  @Transform(({ value }) => value.toUpperCase()) // 将输入转换为大写
  @IsOptional()
  sortOrder?: SortOrderEnum = SortOrderEnum.DESC; // 默认值按 createTime 字段降序，即新数据置顶

  @IsNumber()
  @IsOptional()
  pageNum?: number = 1;

  @IsNumber()
  @IsOptional()
  pageSize?: number = 20;
}

export class UpdateUserDto extends PartialType(CreateUserDto) {
  @IsNumber()
  id: number;
}

export class RemoveUserDto extends PartialType(CreateUserDto) {
  @IsNumber()
  id: number;
}

export class CreateBatchUserDto {
  @IsArray()
  @Type(() => CreateUserDto)
  list: CreateUserDto[];

  @IsBoolean()
  @IsOptional()
  cover?: boolean = false;
}

export class RemoveBatchUserDto {
  @IsArray()
  ids: number[];
}
